package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Long friendId) {
        Friend friend = new Friend();
        friend.setUserId(userId);
        friend.setFriendId(friendId);
        friend.setId(new ObjectId());
        friend.setCreated(new Date().getTime());

        Query query = new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        if (!mongoTemplate.exists(query, Friend.class)) {
            mongoTemplate.save(friend);
        }

        Friend friend1 = new Friend();
        friend1.setUserId(friendId);
        friend1.setFriendId(userId);
        friend1.setId(new ObjectId());
        friend1.setCreated(new Date().getTime());
        Query query1 = new Query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        if (!mongoTemplate.exists(query1, Friend.class)) {
            mongoTemplate.save(friend1);
        }

    }

    @Override
    public PageResult queryContacts(Long userId, Integer page, Integer pagesize) {

        Query query = new Query(Criteria.where("userId").is(userId));
        query.limit(pagesize).skip((page - 1) * pagesize);
        query.with(Sort.by(Sort.Direction.DESC, "created"));

        List<Friend> list = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);

        return new PageResult(page, pagesize, (int) count, list);
    }

    @Override
    public Integer queryCounts(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        Integer eachLoveCount = friendList.size();
        return eachLoveCount;
    }

    @Override
    public boolean queryLove(Long userId) {
        Query query = new Query(Criteria.where("freindId").is(userId));
        return mongoTemplate.exists(query, Friend.class);
    }

    @Override
    public boolean queryToLike(Long userId, Long deleteId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("friendId").is(deleteId));
        return mongoTemplate.exists(query, Friend.class);
    }

    @Override
    public void deleteLike(Long userId, Long deleteId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("friendId").is(deleteId));
        mongoTemplate.remove(query, Friend.class);
    }


}
